module rank {

    export class BigRankItem extends egret.DisplayObjectContainer {


        public static WIDTH = 550;
        public static HEIGHT = 88;

        private titleTxt: egret.TextField;


        public static readonly VISIBLE: number = 0;
        public static readonly INVISIBLE: number = 1;

        private bgVisible: number = 0;

        private rankNmTxt: egret.TextField;

        private headImg: egret.Bitmap;

        private nickTxt: egret.TextField;

        private scoreTxt: egret.TextField;

        private bgImg: egret.Bitmap;

        public dispose(): void {
            if (this.imageLoader) {
                this.imageLoader.removeEventListener(egret.Event.COMPLETE, this.loadCompleteHandler, this);
            }
            this.imageLoader = null;
        }

        public constructor(info: ScoreItem, bgVisible: number) {
            super();
            this.info = info;
            this.bgVisible = bgVisible;
            if (info != null) {
                this.init();
            }
        }

        private init(): void {
            // this.bgImg = manager.ResManager.getBitmap("rank_sheet.rank_item");
            // this.addChild(this.bgImg);
            // this.bgImg.visible = false

            // if (this.bgVisible == BigRankItem.INVISIBLE) {
            //     this.bgImg.visible = false;
            // }

            let info = this.info;
            this.rankNmTxt = new egret.TextField();
            this.rankNmTxt.size =  60;
            let rankIdx:number = info.rankNb;
            if(rankIdx<2) {
                this.rankNmTxt.textColor = 0xff0000;
            } else if(rankIdx<3) {
                this.rankNmTxt.textColor = 0xffa800;
            } else if(rankIdx<4) {
                this.rankNmTxt.textColor = 0xfffc00;
            } else {
                this.rankNmTxt.textColor = 0x4d4d4d;
            }

            this.rankNmTxt.text = info.rankNb.toString();
            this.addChild(this.rankNmTxt);

            this.headImg = new egret.Bitmap();
            this.headImg.width = this.headImg.height = 65;
            this.addChild(this.headImg);

            if (info.nick.length > 10) {
                info.nick = info.nick.substr(0, 10);
            }

            this.nickTxt = new egret.TextField();
            // this.nickTxt.cacheAsBitmap = true;
            this.nickTxt.textColor = 0x0;
            this.nickTxt.text = info.nick;
            this.addChild(this.nickTxt);
            this.nickTxt.size = 20;
            this.nickTxt.bold = true;


            this.scoreTxt = new egret.TextField();
            this.scoreTxt.size = 40;
            this.scoreTxt.text = info.score.toString();
            this.addChild(this.scoreTxt);


            let headX: number = 80;
            this.rankNmTxt.x = 10;

            this.headImg.x = headX + 50;

            this.nickTxt.x = this.headImg.x + this.headImg.width + 20 ;

            this.scoreTxt.x = BigRankItem.WIDTH - this.scoreTxt.width - 130 ;

            this.rankNmTxt.x = headX - this.rankNmTxt.width + 100>> 1;
            this.rankNmTxt.y = BigRankItem.HEIGHT - this.rankNmTxt.height >> 1;
            this.headImg.y = BigRankItem.HEIGHT - this.headImg.height >> 1;
            this.nickTxt.y = BigRankItem.HEIGHT - this.nickTxt.height >> 1;
            this.scoreTxt.y = BigRankItem.HEIGHT - this.scoreTxt.height >> 1;
            this.scoreTxt.textColor = 0xcb6405;
            this.loadImg();
        }


        private info: ScoreItem;
        private imageLoader: egret.ImageLoader;

        public loadImg() {
            this.imageLoader = new egret.ImageLoader();
            this.imageLoader.addEventListener(egret.Event.COMPLETE, this.loadCompleteHandler, this);
            this.imageLoader.load(this.info.avatarUrl);
        }

        private loadCompleteHandler(event: egret.Event): void {
            let imageLoader = <egret.ImageLoader>event.currentTarget;
            imageLoader.removeEventListener(egret.Event.COMPLETE, this.loadCompleteHandler, this);
            let texture = new egret.Texture();
            texture._setBitmapData(imageLoader.data);
            this.headImg.texture = texture;
        }

    }

}